package com.ubtechinc.cruzr.launcher2.model.db;

import android.content.Context;
import android.content.SharedPreferences;

import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.model.dao.FilterDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InitialTools {
    private static final String SHARED_PATH = "launcher2";
    private static SharedPreferences sharedPreferences;

    public static void insertOrigialData() {
        Context context = LauncherApplication.getContext();
        String filter[] = context.getResources().getStringArray(R.array.admini_list);
        List<AppInfo> list = new ArrayList<>();
        for (int i = 0; i < filter.length; i++) {
            String temp = filter[i];
            String sp[] = temp.split("#");
            AppInfo info;
            //从数据库获取app信息--可以使用room替换
            AppInfo extrainfo = FilterDao.get().getAppInfoForId(i);
            if (extrainfo == null) {
                info = new AppInfo();
                info.id = i;
            } else {
                info = extrainfo;
            }
            info.packagename = sp[0];
            info.activityname = sp[0] + "." + sp[1];
            list.add(info);
        }
        try {
            FilterDao.get().createOrUpdateFilteList(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static SharedPreferences getDefaultSharedPreferences(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SHARED_PATH, Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putInt(key, value);
        edit.apply();
    }

    public static int getInt(Context context, String key) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        return sharedPreferences.getInt(key, 0);
    }

}
